var Pagination = {

    showPageCount: 7,
    
    pageCount:0,
    
    currentPage:0,

    init: function (obj, callback) {
        this.bindListener(obj, callback);
    },
    createHtml: function (pageIndex, recordCount, pageSize) {
    	currentPage=pageIndex;
        pageCount = Math.ceil(recordCount / pageSize);  //总页数
        var showPageCount = this.showPageCount;
        var MaxCount = 10000000000;
        var HalfPageCount = (showPageCount + 1) / 2;
        var html = [];

        if (pageCount > MaxCount) {
            pageCount = MaxCount;
        }
        if (pageIndex > pageCount - 1) {
            pageIndex = pageCount - 1;
        }
        html.push("<div class=\"total-number\">共计<span class=\"total-count\">"+recordCount+"</span>条</div>")
        if (pageIndex > 0) {
            html.push("<span class=\"previous\"><a href=\"javascript:;\" page= " + (pageIndex - 1) + " data-rec=\"上一页\">上一页</a></span>");
        }
        else {
            html.push("<span class=\"disable previous\">上一页</span>");
        }
        if (pageCount <= showPageCount) {
            for (var i = 0; i < pageCount; i++) {
                if (pageIndex == i) {
                    html.push("<span class=\"current num\">" + (i + 1) + "</span>");
                }
                else {
                    html.push("<span class=\"num\"><a href=\"javascript:;\"  page=" + i + ">" + (i + 1) + "</a></span>");
                }
            }
        }
        else if (pageIndex < HalfPageCount) {
            for (var i = 0; i < showPageCount - 1; i++) {
                if (pageIndex == i) {
                    html.push("<span class=\"current num\">" + (i + 1) + "</span>");
                }
                else {
                    html.push("<span class=\"num\"><a href=\"javascript:;\"  page=" + i + ">" + (i + 1) + "</a></span>");
                }
            }

            html.push("<span class=\"dots\">...</span>");
            html.push("<span class=\"num\"><a href=\"javascript:;\"  page=" + (pageCount - 1) + ">" + pageCount + "</a></span>");
        }
        else if (pageIndex >= pageCount - HalfPageCount - 1) {
            html.push("<span class=\"num\"><a href=\"javascript:;\" page='0'>" + 1 + "</a></span>");
            html.push("<span class=\"dots\">...</span>");
            for (var i = pageCount - showPageCount + 1; i < pageCount; i++) {
                if (pageIndex == i) {
                    html.push("<span class=\"current num\">" + (i + 1) + "</span>");
                }
                else {
                    html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + i + ">" + (i + 1) + "</a></span>");
                }
            }
        }
        else {
            html.push("<span class=\"num\"><a href=\"javascript:;\" page=0>" + 1 + "</a></span>");
            html.push("<span class=\"dots\">...</span>");

            for (var i = pageIndex - HalfPageCount / 2; i <= pageIndex + HalfPageCount / 2; i++) {
                if (pageIndex == i) {
                    html.push("<span class=\"current num\">" + (i + 1) + "</span>");
                }
                else {
                    html.push("<span class=\"num\"><a href=\"javascript:;\" page=" + i + ">" + (i + 1) + "</a></span>");
                }
            }
            html.push("<span class=\"dots\">...</span>");
            html.push("<span class=\"num\"><a href=\"javascript:;\" page =" + (pageCount - 1) + ">" + pageCount + "</a></span>");
        }

        if (pageIndex < pageCount - 1) {
            html.push("<span class=\"next\"><a href=\"javascript:;\" page=" + (pageIndex + 1) + " data-rec=\"下一页\">下一页</a></span>");
        }
        else {
            html.push("<span class=\"disable next\">下一页</span>");
        }

        html.push("<span class=\"total\">每页" + pageSize + "条</span>");
        html.push("<span class=\"total total_page\">共" + pageCount + "页</span>");
        html.push("<span class=\"page_jump\">到</span><input id='pageInput' oldpage='' value='"+(Number(pageIndex)+1)+"' maxlength='" + pageCount + "' type='text' ><span class=\"page_jump\">页</span><button class='jump' type='button' id='pagebtn'>确定</button>");

        // html.push("<script>pagination(" + pageCount + ",$('#pageInput'), $('#pagebtn'));</script>");
        return html.join("");
    },

    bindListener: function (obj, callback) {
        obj.on("click", "a", function () {
            if (typeof callback === "function") {
                var index = $(this).attr("page");
                callback(parseInt(index));
            }
            return false;
        });
        obj.on("change", "input", function () {
            var jumpPage = $("#pageInput").val();
            var reg = /^[0-9]*$/;
            if (jumpPage!=null && !reg.test(jumpPage)) {
            	$("#pageInput").val(currentPage+1);
            	return;
            }
            var page=parseInt($("#pageInput").val());
            if(page>pageCount){
            	$("#pageInput").val(pageCount);
            }
            
        });
        obj.on("keyup", "input", function (event) {
        	if(event.which==13){
        		if (typeof callback === "function") {
                    var index = $("#pageInput").val();
                    callback(parseInt(index)-1);
                }
        	};
        });
        obj.on("click", "button", function () {
        	if (typeof callback === "function") {
                var index = $("#pageInput").val();
                callback(parseInt(index)-1);
            }
            return false;
        })
    },
    Page: function (obj, pageIndex, recordCount, pageSize) {
        obj.empty();
        obj.html(this.createHtml(pageIndex, recordCount, pageSize));
    }

}
